//
//  JourneyButtonView.m
//  ITrip
//
//  Created by 1564335 on 15-6-2.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JourneyButtonView.h"
#import "JourneyButtonTableViewCell.h"
@implementation JourneyButtonView

- (instancetype)initWithFrame:(CGRect)frame :(StrategyPagesParser *)pages {
    self = [super initWithFrame:frame];
    if (self) {
        self.pages = [[StrategyPagesParser alloc] init];
        self.pages = pages;
        [self createTableView];
    }
    return self;
}

- (void)createTableView
{
    UITableView *journeyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 64) style:UITableViewStylePlain];
    [self addSubview:journeyTableView];
    [journeyTableView release];
    journeyTableView.delegate = self;
    journeyTableView.dataSource = self;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier = @"cell";
    JourneyButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[JourneyButtonTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.contentView.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, self.frame.size.width, cell.frame.size.height);
    }
    
    cell.titleLabel.text = nil;
    cell.descriptionLabel.text = nil;
    cell.image_url = nil;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    for (UIImageView *view in cell.imageScrollView.subviews) {
        [view removeFromSuperview];
    }
    StrategyChildernParser *childern = [self.pages.children objectAtIndex:indexPath.section];
    StrategySectionsParser *sections = [childern.sections objectAtIndex:indexPath.row];
    [cell setSectionsParser:sections];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyChildernParser *childern = [self.pages.children objectAtIndex:indexPath.section];
    StrategySectionsParser *sections = [childern.sections objectAtIndex:indexPath.row];
    
    if (sections.strategy_description != nil) {
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:20] forKey:NSFontAttributeName];
        CGRect frame = [sections.strategy_description boundingRectWithSize:CGSizeMake(self.frame.size.width - 40, 3000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        if (sections.photos.count != 0) {
            return 70 + frame.size.height + 150;
        } else {
            return 70 + frame.size.height;
        }
        
    } else {
        if (sections.photos.count != 0) {
            return 70 +  150;
        } else {
            return 70 ;
        }
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    
    return self.pages.children.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    StrategyChildernParser *childern = [self.pages.children objectAtIndex:section];
    return childern.sections.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    StrategyChildernParser *childern = [self.pages.children objectAtIndex:section];
    return childern.strategy_second_title;
}

@end
